﻿namespace PlugNT.Common.Http
{
    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Runtime.CompilerServices;
    using System.Xml;
    using PlugNT.Safe;

    public class SiteUrls
    {
        private bool _CheckRequestArgs;
        private bool _Enable;
        private NameValueCollection _Paths = new NameValueCollection();
        private ArrayList _Urls = new ArrayList();
        private static volatile SiteUrls instance = null;
        private static object lockHelper = new object();
        private static string SiteUrlsFile = (IConfig.ConfigPath + "reurl.config");

        private SiteUrls()
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(SiteUrlsFile);
            XmlNode tmpnode1 = xml.SelectSingleNode("reurl/enable");
            if (tmpnode1 != null)
            {
                this._Enable = tmpnode1.InnerXml.Trim().Equals("true");
            }
            XmlNode tmpnode2 = xml.SelectSingleNode("reurl/checkrequestargs");
            if (tmpnode2 != null)
            {
                this._CheckRequestArgs = tmpnode2.InnerXml.Trim().Equals("true");
            }
            XmlNode root = xml.SelectSingleNode("reurl/urls");
            foreach (XmlNode n in root.ChildNodes)
            {
                if ((n.NodeType != XmlNodeType.Comment) && (n.Name.ToLower() == "rewrite"))
                {
                    XmlAttribute name = n.Attributes["name"];
                    XmlAttribute path = n.Attributes["path"];
                    XmlAttribute page = n.Attributes["page"];
                    XmlAttribute querystring = n.Attributes["querystring"];
                    XmlAttribute pattern = n.Attributes["pattern"];
                    if ((((name != null) && (path != null)) && ((page != null) && (querystring != null))) && (pattern != null))
                    {
                        this.Paths.Add(name.Value, path.Value);
                        this._Urls.Add(new URLRewrite(name.Value, pattern.Value, page.Value.Replace("^", "&"), querystring.Value.Replace("^", "&")));
                    }
                }
            }
        }

        public static SiteUrls GetSiteUrls()
        {
            if (instance == null)
            {
                lock (lockHelper)
                {
                    if (instance == null)
                    {
                        instance = new SiteUrls();
                    }
                }
            }
            return instance;
        }

        public bool CheckRequestArgs
        {
            get
            {
                return this._CheckRequestArgs;
            }
            set
            {
                this._CheckRequestArgs = value;
            }
        }

        public bool Enable
        {
            get
            {
                return this._Enable;
            }
            set
            {
                this._Enable = value;
            }
        }

        public NameValueCollection Paths
        {
            get
            {
                return this._Paths;
            }
        }

        public ArrayList Urls
        {
            get
            {
                return this._Urls;
            }
        }

        public class URLRewrite
        {
            private string _Name;
            private string _Page;
            private string _Pattern;
            private string _QueryString;

            public URLRewrite(string name, string pattern, string page, string querystring)
            {
                this._Name = name;
                this._Pattern = pattern;
                this._Page = page;
                this._QueryString = querystring;
            }

            public string Name
            {
                get
                {
                    return this._Name;
                }
                set
                {
                    this._Name = value;
                }
            }

            public string Page
            {
                get
                {
                    return this._Page;
                }
                set
                {
                    this._Page = value;
                }
            }

            public string Pattern
            {
                get
                {
                    return this._Pattern;
                }
                set
                {
                    this._Pattern = value;
                }
            }

            public string QueryString
            {
                get
                {
                    return this._QueryString;
                }
                set
                {
                    this._QueryString = value;
                }
            }
        }
    }
}

